package util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;

public class Reflections {

	public static Field getField(final Class<?> clazz, final String fieldName) throws Exception {
        Field field = null;
        try {
            field = clazz.getDeclaredField(fieldName);
            if (field == null && clazz.getSuperclass() != null) {
                field = getField(clazz.getSuperclass(), fieldName);
            }
        } catch (Exception e) {
            field = getField(clazz.getSuperclass(), fieldName);
        }
        if (field != null) {
            field.setAccessible(true);
            return field;
        } else {
            return null;
        }
	}

	public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception {
		final Field field = getField(obj.getClass(), fieldName);
		field.set(obj, value);
	}

	public static Object getFieldValue(final Object obj, final String fieldName) throws Exception {
		final Field field = getField(obj.getClass(), fieldName);		
		return field.get(obj);
	}

	public static Constructor<?> getFirstCtor(final String name) throws Exception {
		final Constructor<?> ctor = Class.forName(name).getDeclaredConstructors()[0];
	    ctor.setAccessible(true);
	    return ctor;
	}

}
